<?php
require_once("lib.php");

// Basic set has occured.
// Use $_SESSION["authed"] to test for auth
// Use $db to access mysqli API to database


// Not logged in
if (!$_SESSION["authed"]) {
    header("Location: index.php");
}

if (isset($_POST["newTrackTag"]) && trim($_POST["newTrackTag"]) != "") {
    $newTag = trim($_POST["newTrackTag"]);
    $stmt = $db->prepare("INSERT INTO track_tags (trackTag) VALUES (:tag)");
    if (!$stmt->execute(array("tag" => $newTag))) {
        $stmt->debugDumpParams();
    }
}

if (isset($_POST["delete"])) {
    foreach ($_POST["delete"] as $tagID => $flag) {
        $stmt = $db->prepare("DELETE FROM track_tags WHERE id = :tagID");
        if (!$stmt->execute(array("tagID" => $tagID))) {
            $stmt->debugDumpParams();
        }
    }
}

if (isset($_POST["update"])) {
    foreach ($_POST["update"] as $tagID => $newValue) {
        if (trim($newValue) != "") {
            $stmt = $db->prepare("UPDATE track_tags SET trackTag = :trackTag WHERE id = :tagID");
            if (!$stmt->execute(array("trackTag" => $newValue, "tagID" => $tagID))) {
                $stmt->debugDumpParams();
            }
        }
    }
}


$stmt = $db->prepare("SELECT * FROM track_tags");
$tagCheckBoxes = "";
if ($stmt->execute()) {
    while ($row = $stmt->fetch()) {
        $trackTypeID = $row["id"];
        $trackTag = $row["trackTag"];
        $tagCheckBoxes .= <<< HTML
<div class="control-group">
    <div class="controls">
        <input type="checkbox" name="delete[$trackTypeID]">
        <input type="text" value="$trackTag" name="update[$trackTypeID]">
    </div>
</div>
HTML;
    }
} else {
    $stmt->debugDumpParams();
}

$tags = <<<HTML
<form class="form-horizontal" name="formUpdateTrack" action="" method="post">
    <formset>
        <input type="hidden" name="update">
        <legend>Manage Tags</legend>
        <p>Check box(es) to delete tag(s). Modify text to edit. Click 'Update'.</p>
        $tagCheckBoxes
        <div class="control-group">
            <label class="control-label" for="newTrackTag">New Tag</label>
            <div class="controls">
                <input type="text" id="newTrackTag" name="newTrackTag" placeholder="New Tag...">
            </div>
        </div>
        <button type="submit" class="btn">Update</button>
    </formset>
</form>
HTML;

$htmlTitle = "Hikes and Things: manage tags";
echo <<<CONTENT
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>$htmlTitle</title>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
        <link href="style.css" rel="stylesheet">
        $analytics
    </head>
    <body>
        <div class="navbar navbar-static">
            <div class="navbar-inner">
                <div class="container" style="width: auto;">
                    <a class="brand" href="#">Manage Tags</a>
                    <ul class="nav" role="navigation">
                        <li class="divider-vertical"></li>
                        <li><a href="index.php">Tracks</a></li>
                    </ul>
                    <ul class="nav pull-right">
                        <li class="divider-vertical"></li>
                        <li>$logInOut</li>
                    </ul>
                </div>
            </div>
        </div>
        $tags
        <script src="jquery-1.9.1.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
        <script src="bootbox.min.js"></script>
    </body>
</html>
CONTENT;
?>
